{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# <center>第2章 Python编程基础</center>\n",
    "\n",
    "<br>\n",
    "\n",
    "- [2.1 变量与表达式](#2.1-变量与表达式)\n",
    "  - [2.1.1 变量的定义与赋值](#2.1.1-变量的定义与赋值)\n",
    "  - [2.1.2 算术表达式](#2.1.2-算术表达式)\n",
    "  - [2.1.3 关系表达式与逻辑表达式](#2.1.3-关系表达式与逻辑表达式)\n",
    "  - [2.1.4 海象运算符](#2.1.4-海象运算符)\n",
    "  - [2.1.5 运算符的优先级](#2.1.5-运算符的优先级)\n",
    "- [2.2 语句](#2.2-语句)\n",
    "  - [2.2.1 简单语句](#2.2.1-简单语句)\n",
    "  - [2.2.2 复合语句与空语句](#2.2.2-复合语句与空语句)\n",
    "  - [2.2.3 注释](#2.2.3-注释)\n",
    "- [2.3 数据类型](#2.3-数据类型)\n",
    "  - [2.3.1 基本数据类型](#2.3.1-基本数据类型)\n",
    "  - [2.3.2 空类型](#2.3.2-空类型)\n",
    "  - [2.3.3 扩展数据类型](#2.3.3-扩展数据类型)\n",
    "- [2.4 流程控制](#2.4-流程控制)\n",
    "  - [2.4.1 结构化程序设计](#2.4.1-结构化程序设计)\n",
    "  - [2.4.2 选择](#2.4.2-选择)\n",
    "  - [2.4.3 循环](#2.4.3-循环)\n",
    "- [2.5 模块与包](#2.5-模块与包)\n",
    "  - [2.5.1 模块和包的导入](#2.5.1-模块和包的导入)\n",
    "  - [2.5.2 自定义包*](#2.5.2-自定义包*)\n",
    "  - [2.5.3 常用内置模块*](#2.5.3-常用内置模块*)\n",
    "- [2.6 Python编程规范](#2.6-Python编程规范)\n",
    "  - [2.6.1 规范编码的重要性](#2.6.1-规范编码的重要性)\n",
    "  - [2.6.2 PEP8规范（Python Enhancement Proposal）](#2.6.2-PEP8规范（Python-Enhancement-Proposal）)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 2.1 变量与表达式\n",
    "\n",
    "### 2.1.1 变量的定义与赋值\n",
    "\n",
    "#### 变量定义\n",
    "- 变量名\n",
    "  - 以字母或下划线开头，由字母、数字或下划线组成\n",
    "  - 不能以Python关键字作为变量名\n",
    "  - 不要使用特殊变量名（以`__`）开始和结尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = 1\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'__main__'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "__name__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 动态类型\n",
    "  - 在变量定义时无需指定变量的类型\n",
    "  - 同一个变量可以被赋予完全不同类型的取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'float'>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "x = 1\n",
    "print(type(x))\n",
    "x = 1.1\n",
    "print(type(x))\n",
    "x = 'abc' \n",
    "print(type(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 类型注解（Type Hint）\n",
    "  - 非强制性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "x: int = 1\n",
    "x: float = 1.1\n",
    "x: str = 'python'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### 变量赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = y = z = 1\n",
    "x, y, z = 1, 2, 3\n",
    "x, y = y, x  # 交换数值 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 2.1.2 算术表达式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "__算术运算符__\n",
    "\n",
    "| 运算符 | 功能 | 增强运算符 |\n",
    "| :----: | :--: | :--------: |\n",
    "|  `+`   |  加  |    `+=`    |\n",
    "|  `-`   |  减  |    `-=`    |\n",
    "|  `*`   |  乘  |    `*=`    |\n",
    "|  `/`   |  除  |    `/=`    |\n",
    "|  `**`  | 乘方 |   `**=`    |\n",
    "|  `//`  | 整除 |   `//=`    |\n",
    "|  `%`   | 求余 |    `%=`    |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 1\n",
    "x = x % 2\n",
    "x "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "23"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 + 4 * 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.3333333333333335"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 / 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 // 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 % 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 2.1.3 关系表达式与逻辑表达式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "__关系运算符__\n",
    "\n",
    "| 运算符 |   功能   |\n",
    "| :----: | :------: |\n",
    "|  `>`   |   大于   |\n",
    "|  `<`   |   小于   |\n",
    "|  `>=`  | 大于等于 |\n",
    "|  `<=`  | 小于等于 |\n",
    "|  `==`  | 逻辑相等 |\n",
    "|  `!=`  |  不等于  |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "__逻辑运算符__\n",
    "\n",
    "|  运算符  |                功能                |\n",
    "| :------: | :--------------------------------: |\n",
    "|  `and`   |               逻辑与               |\n",
    "|   `or`   |               逻辑或               |\n",
    "|  `not`   |               逻辑非               |\n",
    "|   `is`   |        判断两个对象是否相同        |\n",
    "| `is not` |       判断两个对象是否不相同       |\n",
    "|   `in`   |  判断一个对象是否在一个容器对象中  |\n",
    "| `not in` | 判断一个对象是否不在一个容器对象中 |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y = 1, 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x == 1 and y == 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x < y < 0 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 2.1.4 海象运算符\n",
    "\n",
    "- Python 3.8中加入了一种新的运算符`:=`，称为海象运算符\n",
    "  - 能够在计算逻辑表达式的同时，将表达式的一部分赋值给一个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(x:=1) > (y:=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 相当于："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 1\n",
    "y = 2\n",
    "x > y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 2.1.5 运算符的优先级\n",
    "  \n",
    "- 逻辑运算符 > 比较运算 > 算术运算符\n",
    "- 合理使用`()`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [9, 5, 2, 7]\n",
    "(2 in l) and (6 > (len(l) + 1) >= 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 2.2 语句\n",
    "\n",
    "### 2.2.1 简单语句\n",
    "\n",
    "- 表达式\n",
    "- 输出语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Python!\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello Python!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x= 1 , y= 2 abc\n"
     ]
    }
   ],
   "source": [
    "x, y = 1, 2\n",
    "print(\"x=\", x, \", y=\", y, 'abc')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 输入语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入数据：1\n",
      "1\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "s = input(\"请输入数据：\")\n",
    "print(s)\n",
    "print(type(s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 函数调用\n",
    "- 删除对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'x' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-8-3dde88e24a37>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'x' is not defined"
     ]
    }
   ],
   "source": [
    "x = 10\n",
    "del x\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 导入模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.2246467991473532e-16"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "math.pi\n",
    "math.sin(math.pi)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 2.2.2 复合语句与空语句\n",
    "\n",
    "- 复合语句（语句块）\n",
    "  - C/C++、C#、Java中使用`{}`\n",
    "  - VB中使用`begin ... end`\n",
    "  - Python使用代码的缩进来标识出一个语句块\n",
    "    - 连续多条具有相同缩进的语句，会被解释器判断为隶属于一个语句块\n",
    "    - 代码的缩进是强制性的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello\n",
      "Python\n"
     ]
    }
   ],
   "source": [
    "if True:\n",
    "    print('Hello')\n",
    "    print('Python') \n",
    "    if True:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 语句换行\n",
    "  - 过长的语句也会影响代码的可读性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 > 3 and \\\n",
    "1 < 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 > 3 and (\n",
    "    1 < 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 空语句\n",
    "  - `pass`\n",
    "  - 用于标识出空语句块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "if True:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    " ### 2.2.3 注释\n",
    "\n",
    "- 单行注释\n",
    "  - `#`\n",
    "- 多行注释\n",
    "  - `'''`\n",
    "  - `\"\"\"`\n",
    "- 特殊注释\n",
    "  - `#!/usr/bin/python`、`#!/usr/bin/env/python`或其他解释器所在路径\n",
    "  - `# -*- coding: utf-8 -*-` 或 `# coding=utf-8`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 2.3 数据类型\n",
    "\n",
    "### 2.3.1 基本数据类型\n",
    "\n",
    "\n",
    "<center>Python基本数据类型</center>\n",
    "\n",
    "| 类型 | 表示符                    | 示例                     |\n",
    "| ---- | ------------------------- | ------------------------ |\n",
    "| 字符 | `str`                     | `'Hello Python'`         |\n",
    "| 数值 | `int`, `float`, `complex` | `1`, `1.0`, `1+2j`       |\n",
    "| 序列 | `list`, `tuple`           | `[1, 2, 3]`, `(1, 2, 3)` |\n",
    "| 字典 | `dict`                    | `{'x': 1, 'y': 2}`       |\n",
    "| 集合 | `set`, `frozenset`        | `{1, 2, 3}`              |\n",
    "| 布尔 | `bool`                    | `True`, `False`          |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 字符类型\n",
    "  - `'Hello Python'`\n",
    "  - `\"Hello Python\"`\n",
    "  - `'''Hello Python'''`\n",
    "  - `\"\"\"Hello Python\"\"\"`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "   Hello\n",
      "   Python\n",
      "\n"
     ]
    }
   ],
   "source": [
    "s = \"Hello' Python\"\n",
    "s = 'Hello\" Python'\n",
    "s = '''\n",
    "   Hello\n",
    "   Python\n",
    "'''\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 特殊字符串\n",
    "  - `r'Hello Python'`\n",
    "  - `u'Hello Python'`\n",
    "  - `b'Hello Python!'`\n",
    "  - `f'Hello Python'`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 数值类型\n",
    "  - `int`\n",
    "  - `float`\n",
    "  - `complex`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1j ** 2\n",
    "c = complex(1 + 2j)\n",
    "type(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 序列类型\n",
    "  - `list`：列表，可变类型\n",
    "  - `tuple`：元组，不可变类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6, 5]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [9, 5, 2, 7, 'abc']\n",
    "l[0] = 6  # 索引\n",
    "print(l[0:2])    # 切片\n",
    "type(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "(9, 5)\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-15-ed2d24a5a67e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "t = (9, 5, 2, 7)\n",
    "print(t[0])\n",
    "print(t[0:2])\n",
    "t[0] = 6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 序列解包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 7]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, y, z = 9, 5, 2\n",
    "x, y, *z = (9, 5, 2, 7)\n",
    "z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 临时变量`_`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, *_ = (9, 5, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 集合类型\n",
    "  - 元素无序\n",
    "  - 集合运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2, 5, 7, 9}\n",
      "{2, 5}\n",
      "<class 'list'> <class 'set'>\n",
      "{1, 2, 3}\n"
     ]
    }
   ],
   "source": [
    "s1 = {9, 5, 2}\n",
    "s2 = {5, 2, 7}\n",
    "print(s1 | s2)\n",
    "print(s1 & s2)\n",
    "\n",
    "l = [1, 1, 2, 2, 3, 3]\n",
    "s = set(l)\n",
    "print(type(l), type(s))\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 字典类型\n",
    "  - 元素由key-value对组成\n",
    "  - 使用key访问元素的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'x': 1, 'y': 2, 'z': 3}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "d = {'x': 1, 'y': 2}\n",
    "d['z'] = 3\n",
    "print(d)\n",
    "\n",
    "l = dict()\n",
    "print(type(l))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 布尔类型\n",
    "  - 取值为`True`、`False`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 2.3.2 空类型\n",
    "  - `None`\n",
    "  - 判断一个变量的取值是否为空要使用`is`或`is not`关键字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "NoneType"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = None\n",
    "print(x is None)\n",
    "print(x is not None)\n",
    "type(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 2.3.3 扩展数据类型\n",
    "\n",
    "- 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fun\n",
      "test\n"
     ]
    }
   ],
   "source": [
    "def fun():\n",
    "    pass\n",
    "print(fun.__name__)\n",
    "fun.__name__ = 'test'\n",
    "print(fun.__name__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Human'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Human:\n",
    "    name = ''\n",
    "    def say(self):\n",
    "        print('My name is', self.name)\n",
    "    def run(self):\n",
    "        self.say()\n",
    "\n",
    "Human.__name__  # 注意__name__与name是不同的属性"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "- 对象（实例）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My name is 张三\n"
     ]
    }
   ],
   "source": [
    "h = Human()\n",
    "h.name = '张三'\n",
    "h.say()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'math'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "math.__name__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 2.4 流程控制\n",
    "\n",
    "### 2.4.1 结构化程序设计\n",
    "\n",
    "- GOTO语句\n",
    "- 结构化程序设计是一种编程范式或编程思维，最早由荷兰计算机科学家Edsger Dijkstra提出\n",
    "- 核心的特征\n",
    "  - 将软件代码划分为子程序（函数或方法）、代码块等模块，再通过流程控制将各模块连接起来\n",
    "- 作用\n",
    "  - 程序具有清晰的逻辑结构\n",
    "  - 提高软件质量\n",
    "  - 降低软件的维护成本\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 三种基本的流程控制结构\n",
    "  - 顺序结构\n",
    "  - 选择结构\n",
    "  - 循环结构"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 2.4.2 选择\n",
    "\n",
    "- 语法\n",
    "\n",
    "```\n",
    "if 逻辑表达式1:\n",
    "    语句块1\n",
    "elif 逻辑表达式2:\n",
    "    语句块2\n",
    "elif 逻辑表达式3:\n",
    "    语句块3\n",
    "...\n",
    "else:\n",
    "    语句块\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个整数：0\n",
      "您输入了零！\n"
     ]
    }
   ],
   "source": [
    "x = int(input(\"请输入一个整数：\"))\n",
    "if x < 0:\n",
    "    print(\"您输入了一个负数！\")\n",
    "elif x == 0:\n",
    "    print(\"您输入了零！\")\n",
    "else:\n",
    "    print(\"您输入了一个正数！\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 海象运算符的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字符串长度为 22 超出了最大长度！\n"
     ]
    }
   ],
   "source": [
    "s = 'Python is interesting!'\n",
    "if (s_len:=len(s)) > 10:\n",
    "    print(\"字符串长度为\", s_len, '超出了最大长度！')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 若不使用海象运算法，则需要运算两次`len`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字符串长度为 22 超出了最大长度！\n"
     ]
    }
   ],
   "source": [
    "s = 'Python is interesting!'\n",
    "if len(s) > 10:\n",
    "    print(\"字符串长度为\", len(s), '超出了最大长度！')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- if..else表达式\n",
    "  - 相当于C/C++语言中的三目运算符\n",
    "  - `if 逻辑表达式 else ...`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个整数：１０\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = int(input(\"请输入一个整数：\"))\n",
    "is_positive = True if x > 0 else False\n",
    "is_positive\n",
    "True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 2.4.3 循环\n",
    "\n",
    "#### `for`循环\n",
    "- 遍历可迭代对象\n",
    "- 语法\n",
    "  ```\n",
    "  for 变量 in 可迭代对象:\n",
    "    语句块\n",
    "  ```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "45\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    "sum_l = 0\n",
    "for n in l:\n",
    "    sum_l += n\n",
    "#     sum_l = sum_l + n\n",
    "print(sum_l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- `range`函数\n",
    "  - `range(n)`：返回一个最小值为0，最大值为n-1的range对象；\n",
    "  - `range(m, n)`：返回一个最小值为m，最大值为n-1的range对象；\n",
    "  - `range(m, n, s)`：返回一个最小值为m，最大值为n-1，步长为s的range对象。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "45\n"
     ]
    }
   ],
   "source": [
    "sum_l = 0\n",
    "for n in range(10):\n",
    "    sum_l += n\n",
    "print(sum_l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2500"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求大于等于1且小于100的奇数之和\n",
    "sum_odd = 0\n",
    "for n in range(1, 100, 2):\n",
    "    sum_odd += n\n",
    "print(sum_odd)\n",
    "sum(range(1, 100, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- `enumerate`函数\n",
    "  - `enumerate`函数能够将一个可迭代对象转换成enumerate对象\n",
    "  - 每个元素都是一个形如`(索引, 值)`的元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python 的第 1 个字母是 P\n",
      "Python 的第 2 个字母是 y\n",
      "Python 的第 3 个字母是 t\n",
      "Python 的第 4 个字母是 h\n",
      "Python 的第 5 个字母是 o\n",
      "Python 的第 6 个字母是 n\n"
     ]
    }
   ],
   "source": [
    "text = 'Python'\n",
    "for i, s in enumerate(text):\n",
    "    print(text, '的第', i+1, '个字母是', s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- `zip`函数\n",
    "  - 像拉链一样将两个序列“咬合”起来\n",
    "  - 接受两个或多个序列对象，返回一个可迭代的`zip`对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三 的分数是 95\n",
      "李四 的分数是 59\n",
      "王五 的分数是 80\n"
     ]
    }
   ],
   "source": [
    "names = ['张三', '李四', '王五']\n",
    "scores = [95, 59, 80]\n",
    "for name, score in zip(names, scores):\n",
    "    print(name, \"的分数是\", score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### `while`循环\n",
    "\n",
    "- 语法\n",
    "  ```\n",
    "  while 条件表达式:\n",
    "    语句块\n",
    "  ```\n",
    "  - 语句块中必须有改变条件表达式的语句或者退出循环的语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sum_value = 0\n",
    "i = 0\n",
    "while i < 10:\n",
    "    sum_value += i\n",
    "    i += 1          # 不要忘记改变循环条件\n",
    "print(sum_value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### `break`与`continue`\n",
    "\n",
    "- `break`\n",
    "  - 提前终止循环\n",
    "- `continue`\n",
    "  - 中止当前的迭代，提前进入到下一轮迭代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "6\n",
      "9\n",
      "12\n",
      "15\n",
      "18\n"
     ]
    }
   ],
   "source": [
    "# 输出20以内所有能被3整除的数\n",
    "i = 0\n",
    "while True:\n",
    "    if i >= 20:\n",
    "        break\n",
    "    i += 1\n",
    "    if i % 3 != 0:\n",
    "        continue\n",
    "    print(i)\n",
    "pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### `else`子句\n",
    "- `for`循环和`while`循环都有一个`else`子句\n",
    "  \n",
    "  - `for`\n",
    "    ```\n",
    "    for 变量 in 可迭代对象:\n",
    "        语句块\n",
    "    else:\n",
    "        语句块\n",
    "    ```\n",
    "\n",
    "  - `while`\n",
    "\n",
    "    ```\n",
    "    while 条件表达式:\n",
    "        语句块\n",
    "    else:\n",
    "        语句块\n",
    "    ```\n",
    "- `else`子句中的语句块，只有循环完整执行完毕时才执行。若使用了`break`语句提前退出循环，则`else`子句中的语句块不会被执行\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 2.5 模块与包\n",
    "\n",
    "### 2.5.1 模块和包的导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n",
      "1.0\n",
      "1.0\n"
     ]
    }
   ],
   "source": [
    "import math               # 方法1：导入模块\n",
    "print(math.sin(0.5 * math.pi))\n",
    "import math as m          # 方法2：导入模块并为其取别名\n",
    "print(m.sin(0.5 * m.pi))\n",
    "from math import pi, sin  # 方法3：导入模块中的函数\n",
    "print(sin(0.5 * pi)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 2.5.2 自定义包*\n",
    "\n",
    "- 包\n",
    "  - Python中的包与普通文件夹的区别在于，作为包的文件夹中包含一个名为`__init__.py`的特殊文件\n",
    "- 相对导入\n",
    "  - `.`表示当前包，`..`表示当前包的父包，`...`表示父包的父包\n",
    "\n",
    "#### 例：一个名为`project`的项目\n",
    "```\n",
    "project\n",
    "├── A\n",
    "│   ├── B\n",
    "│   │   ├── __init__.py\n",
    "│   │   └── module_in_B.py\n",
    "│   ├── __init__.py\n",
    "│   └── module_in_A.py\n",
    "└── module.py\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 在`module.py`中导入变量`a`和`b`\n",
    "\n",
    "  ```python\n",
    "  from A.module_in_A import a\n",
    "  from A.B.module_in_B import b\n",
    "  ```\n",
    "\n",
    "- 在`module_in_A.py`中导入变量`b`\n",
    "\n",
    "  ```python\n",
    "  from .B.module_in_B import b\n",
    "  ```\n",
    "\n",
    "- 在`module_in_B.py`中导入变量`a`\n",
    "\n",
    "  ```python\n",
    "  from ..module_in_A import a\n",
    "  ```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "- 导入包时的查找顺序\n",
    "  1. 查看被导入的是否是内置模块\n",
    "  2. `sys.path`中的路径\n",
    "  3. 若搜索不到就返回导入错误\n",
    "- `sys.path`\n",
    "  ```python\n",
    "  import sys\n",
    "  sys.path.append('package_path')\n",
    "  ```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 2.5.3 常用内置模块*\n",
    "\n",
    "| 模块            | 简介                                                         |\n",
    "| --------------- | ------------------------------------------------------------ |\n",
    "| time            | 日期时间处理模块，包含了常用的日期时间计算与格式化处理的函数。 |\n",
    "| datatime        | 对time模块进行了封装，包含了常用的关于日期时间的类。         |\n",
    "| random          | 包含了常用的随机数生成函数。                                 |\n",
    "| sys             | 包含了访问与控制Python解释器的变量和函数。                   |\n",
    "| os              | 包含了对操作系统进行操作的函数。                             |\n",
    "| collections     | 包含了更丰富的容器类。                                       |\n",
    "| itertools       | 包含了操作可迭代对象的非常有用的函数。                       |\n",
    "| functools       | 包含了关于函数式编程的相关功能函数。                         |\n",
    "| json            | 解析与处理JSON格式数据的工具集合。                           |\n",
    "| XML             | 解析与处理XML格式数据的模块。                                |\n",
    "| pickle          | 序列化模块，用于将Python对象存储在磁盘之中。                 |\n",
    "| re              | 正则表达式模块。                                             |\n",
    "| logging         | 日志处理模块。                                               |\n",
    "| urllib          | 用于HTTP请求、操作URL的模块。                                |\n",
    "| HTMLParser      | 用于解析HTML文档的模块。                                     |\n",
    "| threading       | 用于多线程编程的模块。                                       |\n",
    "| subprocess      | 利用Python运行其他程序，只要是在命令行里能够执行的命令，都可以利用模块在Python中运行。 |\n",
    "| multiprocessing | 多进程编程模块。                                             |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 2.6 Python编程规范\n",
    "\n",
    "### 2.6.1 规范编码的重要性\n",
    "  - 团队合作的需要。软件开发往往是一项团队工作，如果不遵循相同的代码规范，团队合作难以进行；\n",
    "  - 规范性良好的代码有利用避免程序错误；\n",
    "  - 规范性良好的代码能够有效降低维护成本。\n",
    "\n",
    "### 2.6.2 PEP8规范（Python Enhancement Proposal）\n",
    "  - Python语言官方建议的编程规范\n",
    "  - https://www.python.org/dev/peps/pep-0008/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### 代码布局\n",
    "\n",
    "- 缩进\n",
    "  - 使用4个空格的缩进方式\n",
    "  - 不使用Tab，更加不能混合使用空格和Tab\n",
    "- 每行代码长度\n",
    "\n",
    "  - 每行代码长度79个字符\n",
    "  - 在运算符之前断行\n",
    "- 空行\n",
    "  - 顶层函数和类前空2行\n",
    "  - 类内方法前空1行\n",
    "  - 逻辑代码块之间空1行\n",
    "- 导入\n",
    "  - 导入语句位于脚本的顶部\n",
    "  - 每个`import`语句仅导入一个模块\n",
    "  - 可使用`from ... import`导入同一模块中的多个对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### 表达式和语句中的空格\n",
    "\n",
    "- 避免使用多余的空格\n",
    "  - `()`、`[]`、`{}`内部相邻的位置不要有空格\n",
    "  - `,`、`:`、`;`之前不要有空格，之后要有一个空格\n",
    "  - `:`作为切片符号时，前后都不要有空格\n",
    "  - 行尾不要有空格\n",
    "  - 函数参数中的运符算前后不要有空格\n",
    "\n",
    "- 使用空格的情况\n",
    "  - 二元运算符，例如`=`、`==`、`>`等，前后要有一个空格（函数参数列表除外）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### 文档与注释\n",
    "\n",
    "- 与代码相矛盾的注释不如无注释，修改代码时应当先修改其注释\n",
    "- 建议将注释放在位置于代码之前的注释块之中\n",
    "- 谨慎使用行尾注释，若要使用则`#`之前至少有两个空格，之后一个空格\n",
    "- 所有公共模块、函数、类和方法都应当有文档字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### 标识符的命名\n",
    "\n",
    "- 避免使用的命名\n",
    "  - `l`（小写的L）、`O`（字母O）、`I`（大写字母i）\n",
    "  - 无意义的字符串\n",
    "  - 包含非ASCII符号的命名\n",
    "- 变量或对象名\n",
    "  - 所有字母小写，可用下划线分隔多个词\n",
    "- 模块名和包名\n",
    "  - 模块名和包名都应当简短，所有字母小写\n",
    "  - 模块名可包含下划线提高可读性，包名中不要包含下划线\n",
    "- 类名\n",
    "  - 首字母大写\n",
    "- 函数与方法\n",
    "  - 函数名与方法名仅包含小写字母，可用下划线分隔多个词\n",
    "  - 非公开的方法前添加一个下划线\n",
    "- 常量\n",
    "  - 所有字母大写，可用下划线分隔多个词"
   ]
  }
 ],
 "metadata": {
  "author": "liuchen",
  "celltoolbar": "幻灯片",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  },
  "rise": {
   "enable_chalkboard": true,
   "footer": "",
   "progress": true,
   "scroll": true,
   "slideNumber": true
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "url": "https://github.com/hitlic/python_book"
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
